
const inquirer = require('inquirer'); //命令行交互
const path = require('path');
const { exec } = require('child_process');
const { buildCheckbox } = require('../config.js');
const fs = require('fs');
const zip = require('../utils/zip.js');
module.exports = async () => {
    inquirer.prompt([{
        "type": "list",
        "message": "选择需要打包的子项目（单选）:",
        "name": "project",
        "choices": [
            "me",
            "task",
            "shop",
            "invite"
        ],
        "pageSize": 20
    },{
        "type": "list",
        "message": "选择需要打包的环境（单选）:",
        "name": "environment",
        "choices": [
            "production",
            "test"
        ],
        "pageSize": 20
    }]).then(result => { //inquirer 交互问答
        const project = result.project
        const mode = result.environment
        const packages = fs.readdirSync('./src/viewsPackages/')
        if(packages.length>0){
            packages.some(package => {
                console.log(path.join('./src/viewsPackages/',package))
                fs.stat(path.join('./src/viewsPackages/',package),(err,data)=>{
                    if(err){
                        console.log(err);
                    }else{
                        if(data.isDirectory()){
                            console.warn(package,' building....');
                            const exec = require('child_process').exec;
                            let cmdStr = 'npm run build-'+ package  ;
                            if(mode == 'test'){
                                cmdStr = `npm run build-${package}:${mode}`  ;
                            }
                            exec(cmdStr,{ 
                                maxBuffer: 200000*1024  /*stdout和stderr的最大长度*/
                            }, (err, stdout, stderr) => {
                                if (err){
                                    console.log(err);
                                    console.warn(package,' 编译命令执行失败');
                                } else {
                                    console.log(stdout);
                                    zip('./dist/',package,project,mode)
                                    console.warn(package,' 编译命令执行成功');
                                }
                            });
                        }
                    }
                })
                
            })
        }
    })
    
 }

 